package com.sinog.basic.config;

import com.sinog.core.util.DesCryptoGraph;
import com.zaxxer.hikari.HikariDataSource;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.util.Objects;

/**
 * @desc 数据源配置
 * @author lzq
 * @date -
 */
@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
@Getter
@Setter
public class DasourceConfig {

    private String driverClassName;
    private String url;
    private String username;
    private String password;
    private Integer minimumIdle;
    private Integer maximumPoolSize;
    private boolean autoCommit;
    private Long idleTimeout;
    private String poolName;
    private Integer maxLifetime;
    private Integer connectionTimeout;
    private String connectionTestQuery;

    /**
     * 描述
     * @return DataSource
     */
    @Bean
    public DataSource dataSource() {
        HikariDataSource hds = new HikariDataSource();
        String str = password;
        byte[] bytes = DesCryptoGraph.hexStringToByte(str);
        byte[] desCbcDecrypt = DesCryptoGraph.desCbcDecrypt(bytes,DesCryptoGraph.KEY.getBytes());
        hds.setDriverClassName(driverClassName);
        hds.setJdbcUrl(url);
        hds.setUsername(username);
        hds.setPassword(new String(Objects.requireNonNull(desCbcDecrypt)));
        hds.setMinimumIdle(minimumIdle);
        hds.setMaximumPoolSize(maximumPoolSize);
        hds.setAutoCommit(autoCommit);
        hds.setIdleTimeout(idleTimeout);
        hds.setPoolName(poolName);
        hds.setMaxLifetime(maxLifetime);
        hds.setConnectionTimeout(connectionTimeout);
        hds.setConnectionTestQuery(connectionTestQuery);
        return hds;
    }
}